package com.anlogic.sdk.bsp.settings;

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import com.anlogic.sdk.tools.model.OSInfo;

public class SummaryView {

	BspSettingsView parentView;

	private IBspSettingTool bspSettingTool;

	private Composite content;

	private Label osTypeLabel;

	private Combo osVersionCombo;

	private Label osDescText;

	private Label hwSpecLabel;

	private Label processorLabel;

	public SummaryView(BspSettingsView parentView, Composite parent, IBspSettingTool bspSettingTool) {
		this.parentView = parentView;
		this.bspSettingTool = bspSettingTool;
		this.createContentArea(parent);
	}

	public Composite getContent() {
		return this.content;
	}

	public Display getDisplay() {
		return this.content.getDisplay();
	}

	public void createContentArea(Composite parent) {
		this.content = new Composite(parent, SWT.NONE);
		this.content.setLayout((Layout) new GridLayout(1, false));
		this.content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		createOsGroup(this.content);

		createTargetGroup(this.content);

		addListeners();
	}

	public void setFont(Control c, int style, int height, Color color) {
		FontData newFontData = c.getFont().getFontData()[0];
		newFontData.setStyle(style);
		if (height == 0)
			height = newFontData.getHeight();
		newFontData.setHeight(height);
		Font newFont = new Font(getDisplay(), newFontData);
		c.setFont(newFont);
		c.setForeground(color);
	}

	private void createOsGroup(Composite parent) {
		Group g = new Group(parent, SWT.NONE);
		g.setText("Operating System");
		g.setLayout((Layout) new GridLayout(3, false));
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd.verticalIndent = 15;
		g.setLayoutData(gd);
		g.setForeground(new Color(null, 0, 0, 255));
		(new Label(g, SWT.NONE)).setText("OS Type: ");
		this.osTypeLabel = new Label(g, SWT.NONE);
		gd = new GridData(SWT.LEFT, SWT.CENTER, false, false);
		gd.widthHint = 100;
		this.osTypeLabel.setLayoutData(gd);
		this.osTypeLabel.getFont().getFontData()[0].setStyle(SWT.ITALIC);
		this.osDescText = new Label(g, SWT.WRAP | SWT.SINGLE);
		gd = new GridData(SWT.LEFT, SWT.FILL, true, true);
		gd.verticalSpan = 2;
		gd.horizontalIndent = 5;
		this.osDescText.setLayoutData(gd);
		(new Label(g, 0)).setText("OS Version: ");
		this.osVersionCombo = new Combo(g, SWT.DROP_DOWN);
		gd = new GridData(SWT.LEFT, SWT.CENTER, false, false);
		this.osVersionCombo.setLayoutData(gd);
	}

	private void createTargetGroup(Composite parent) {
		Group g = new Group(parent, 0);
		g.setText("Target Hardware");
		g.setLayout((Layout) new GridLayout(2, false));
		GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd.verticalIndent = 15;
		g.setLayoutData(gd);
		g.setForeground(new Color(null, 0, 0, 255));
		(new Label((Composite) g, 0)).setText("Hardware Specification:");
		this.hwSpecLabel = new Label((Composite) g, 0);
		this.hwSpecLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		(new Label((Composite) g, 0)).setText("Processor:");
		this.processorLabel = new Label((Composite) g, 0);
		this.processorLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
	}

	public void viewReady() {
		String hpf = this.bspSettingTool.getPlatform().getHpfPath();
		OSInfo osInfo = this.bspSettingTool.getBspOsInfo();
		String osName = osInfo.getName();
		String osVersion = osInfo.getVersion();

		String proc = this.bspSettingTool.getPlatform().getProcessor();
		String[] osVersions = this.bspSettingTool.getOsVersions(osName);
		int index = Arrays.binarySearch((Object[]) osVersions, osVersion);

		this.osTypeLabel.setText(osName);
		this.osVersionCombo.setItems(osVersions);
		this.osVersionCombo.select(index);
		this.osDescText.setText(osInfo.getDesc());

		this.hwSpecLabel.setText(hpf);
		this.processorLabel.setText(proc);
	}
	
	public void addListeners() {
		this.osVersionCombo.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				String item = osVersionCombo.getItem(osVersionCombo.getSelectionIndex());
				bspSettingTool.setOsVersion(item);
			}
		});
	}
}
